<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Manager extends MY_Before_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->datas ['page_title'] = "管理员管理";
    }

    public function index()
    {
        $this->query_list();
    }


    /**
     * 分页数据
     *
     * @param number $page
     */
    private function query_list($page = 1, $filter = array(), $load_page = TRUE)
    {
        $get = $this->input->get();
        if (!empty ($get)) {
            $filter = array_merge($filter, $get);
        }
        if (isset ($filter ['page']) && $filter ['page'] > 0) {
            $page = $filter ['page'];
        }

        $item_list             = $this->myModel->get_list($filter, $page);
        $this->datas ['lists'] = $item_list ['list'];
        $this->datas ['pager'] = $item_list ['pager'];

        if ($this->datas ['full_page']) {
            $this->load->view($this->dwt_list, $this->datas);
        } else {
            return $this->load->view($this->dwt_list, $this->datas, TRUE);
        }
    }

    /**
     * 接收ajax请求
     * @param unknown $action
     * @param number $item_id
     */
    public function ajax($action, $item_id = 0)
    {
        $datas            = $this->input->get();
        $datas['item_id'] = $item_id;
        $this->process_ajax($action, $datas);
    }

    /**
     * 接收表单post数据
     */
    public function post()
    {
        $post = $this->input->post();
        $this->process_ajax($post['action'], $post);
    }

    /**
     * 处理各种ajax请求
     * @param unknown $action
     * @param unknown $datas
     * @return json
     */
    private function process_ajax($action, $datas = array())
    {
        $content     = '';
        $message     = '';
        $append_data = array();

        $exclude_for_id = array('add', 'insert', 'update', 'query');
        if (isset($datas['id'])) {
            $datas['item_id'] = $datas['id'];
        }
        if (!in_array($action, $exclude_for_id) && (!isset($datas['item_id']) || empty($datas['item_id']))) {
            make_json_error("未指明操作对象！[305:$action]");
        }
        $result['error'] = 0;
        switch ($action) {
            case 'query':
                $this->datas ['full_page'] = FALSE;
                $content                   = $this->query_list();
                break;
            case 'edit':
            case 'add':
                $this->datas['modal_title'] = $action == 'add' ? '添加管理员' : '我的信息';
                $this->datas['form_action'] = $action == 'add' ? 'insert' : 'update';
                $this->datas['full_page']   = false;
                $info                       = array();
                if ($action == 'edit') {
                    $info = $this->myModel->get_info($datas['item_id']);
                }
                $this->datas['info']     = $info;
                $append_data['formData'] = $info;
                $content = $this->load->view($this->dwt_info_modal, $this->datas, true);
                break;
            case 'update':
                if (!empty($datas['new_password'])) {
                    if ($datas['new_password'] == $datas['new_password_cfm']) {
                        $datas["password"] = md5($datas['new_password']);
                    } else {
                        make_json_error("对不起,您输入的两次密码不一致!");
                    }
                }
                unset($datas['new_password']);
                unset($datas['new_password_cfm']);
                $result = $this->myModel->edit_data($datas, $datas['item_id']);
                if (isset($datas["password"])) {
                    $message                 = '您修改了密码，需要重新登录!';
                    $append_data['followUp'] = 'redirect';
                    $append_data['redirect'] = site_url('privilege/logout');
                } else {
                    $append_data['followUp'] = 'queryList';
                }
                break;
            default:
                $result['error']   = 300;
                $result['message'] = "非法操作！[" . $datas['item_id'] . "]";
                break;
        }
        if ($result['error'] > 0) {
            make_json_error($result['message'] . "\r\n[Err:" . $result['error'] . "]");
        } else {
            make_json_result($content, $message, $append_data);
        }
    }

}